const spawn = require("child_process").spawn
const path = require('path')
const fs = require('fs')

const components = [
  'test-component'
]

// 清理
fs.rmSync(path.resolve(__dirname, '../dist'), {
  force: true,
  recursive: true
})

components.forEach(async component => {
  console.log(`=====================build ${component}==================`)
  const compiler = new Promise((resolve) => {
    const processObj = spawn('npx',
      [
        'vue-cli-service', 'build',
        '--target', 'lib', '--name',
        component, '--no-clean', `packages/${component}/index.js`
      ]
      , {
        cwd: path.resolve(__dirname, '../'),
        shell: process.platform === 'win32'
      });

    processObj.stdout.on("data", (thunk) => {
      console.log(thunk.toString())
    })
    processObj.stderr.on("data", (data) => {
      console.log(data.toString())
    })
    processObj.on("close", () => {
      processObj.stdin.end()
      resolve()
    })
  })
  await compiler
})

const processObj = spawn('npm',
  [
    'run', 'build'
  ]
  , {
    cwd: path.resolve(__dirname, '../'),
    shell: process.platform === 'win32'
  });

processObj.stdout.on("data", (thunk) => {
  console.log(thunk.toString())
})
processObj.stderr.on("data", (data) => {
  console.log(data.toString())
})
processObj.on("close", () => {
  processObj.stdin.end()
})
